package com.codeyourlife.controller;

import java.sql.Timestamp;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.codeyourlife.entity.EmailVerify;
import com.codeyourlife.entity.User;
import com.codeyourlife.service.EmailVerifyService;
import com.codeyourlife.service.UserService;

@Controller
public class EmailVerifyController {

	@Autowired
	private EmailVerifyService EmailService;
	@Autowired
	private UserService userservice;

	@RequestMapping(value = "/EmailVerify")
	@ResponseBody
	public EmailVerify EmailVerify(HttpServletRequest request) {
		// 获取信息
		String ask_type = request.getParameter("ask_type");
		String email = request.getParameter("email");
		System.out.println(email + " EmailVerifyController:EmailVerify " + ask_type);

		// 判断邮箱是否已经注册
		EmailVerify emailVerifyResult = new EmailVerify();
		User userexist = userservice.findByName(email);

		if (ask_type.equals("1") && userexist != null) {
			// 注册状态下邮箱已经被注册过；
			emailVerifyResult.setEmail("0");
			return emailVerifyResult;
		} else if (ask_type.equals("2") && userexist == null) {
			// 找回密码状态下邮箱没被注册；
			emailVerifyResult.setEmail("0");
			return emailVerifyResult;
		}

		// 获取验证码
		String code = EmailService.getUUID(6);
		// 赋值
		EmailVerify emailVerify = new EmailVerify();
		emailVerify.setCode(code);
		emailVerify.setSend_type(ask_type);
		emailVerify.setEmail(email);
		Timestamp send_time = new Timestamp(System.currentTimeMillis());
		;
		emailVerify.setSend_time(send_time);

		// save record into database;
		EmailService.addEmailVerifyCode(emailVerify);

		// send email with verify code;
		String subject = "猿生(www.codeyourlife.cn)验证码（60秒内有效）";
		if (ask_type == "1") {
			subject += "注册验证码";
		} else if (ask_type == "2") {
			subject += "找回密码验证码";
		}
		String content = "感谢您对猿生的支持！\n您的验证码为： " + code + "\n\n如有任何以为请联系开发者：miclewang@outlook.com";
		try {
			EmailService.sendTextEmail(email, subject, content);
		} catch (Exception e) {
			System.out.println("EmailVerifyController->sendTextEmail");
			e.printStackTrace();
		}
		// 邮箱没有被注册
		emailVerifyResult.setEmail("1");

		return emailVerifyResult;
	}

	
	@RequestMapping(value = "/EmailVerify_check")
	@ResponseBody
	public String EmailVerify_check(HttpServletRequest request){
		String verifyCode = request.getParameter("security-code");
		String email = request.getParameter("email");
		System.out.println(email + " EmailVerify_check " + verifyCode);
		
		int check;
		try{
		EmailVerify emailVerify = new EmailVerify();
		emailVerify=EmailService.findEmailVerifyByEmail(email,"2");
		if(emailVerify.getCode().equals(verifyCode)){
			EmailService.delEmailVerifyCode(emailVerify.getId());
			check=1;
		}
		else{
			check =-1;
		}
		}catch(Exception e){
			check =0;
			System.out.println("EmailVerify_check "+e.toString());
		}
		return "{\"check\":"+check+"}";
	}
}
